Previous Thread
Next Thread
Print Thread
Rate Thread
#221757 07/27/2002 1:54 PM
Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
I needed to write a simple PHP utility script for importing my UBB.classic avatars into UBB.threads. I prefer running such scripts from the shell (command line), so I don't have to deal with CGI timeouts, etc. I could have used Perl instead, but I'm missing the Perl DBI module for interfacing with MySQL, and didn't want to mess with installing it right now.

You would think that reading and writing stuff to the console would be easy (as it is in Perl or C), but it took me a couple of hours to figure out how to do it in PHP. There were mainly two things I had to do:

1) Call ob_implicit_flush() before doing any I/O. This causes the output buffer to be automatically flushed each time output is performed.

2) Explicitly open standard input. Here's a function I wrote for doing the input. The function readline() supposedly does about the same thing, but that's apparently part of an add-on module that has to be compiled into PHP.
code:
// Output a prompt string to the console,
// and get the (whitespace-trimmed) response.

function getline($prompt) {

echo $prompt;
$fp = fopen('php://stdin', 'r') or die("fopen(stdin)\n");
$line = fgets($fp, 1024);
fclose($fp);
return trim($line);
}



If anyone knows an easier way of doing this, please correct me.

Sponsored Links
joeuser #221758 07/29/2002 1:40 PM
Joined: May 1999
Posts: 1,715
Addict
Addict
Joined: May 1999
Posts: 1,715
It is a hassle to use PHP for command-line programs, since it is originally meant for webscripts only, but they are working on making it easier. Since 4.2.0 it has been possible to compile a cli-version binary but it has been experimental. From 4.3.0 (development version) it is no longer experimental and is always built.

I have tried this CLI binary for 4.2.2 once or twice and it seems to work.

With this implicit_flush variable of php.ini is overridden so that nothing is buffered. The max execution time is set to infinite, html_errors is set to false and the $argc and $argv variables are set with the arguments sent to the program. In addition to this, STDIN, STDOUT and STDERR are always opened.

More information can be found here.

c0bra #221759 07/30/2002 4:40 PM
Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
Thanks. I had so much trouble getting PHP upgraded to 4.1.2 on my server, I'm scared to try to upgrade it.

I'll work up the courage some day.

The hard part about console I/O in PHP was figuring out how to do it. Once I did that, the actual process isn't really any harder than in C or Perl.

joeuser #221760 07/30/2002 7:51 PM
Joined: May 1999
Posts: 1,715
Addict
Addict
Joined: May 1999
Posts: 1,715
Yeah, most things are hard when you don't know how to do it. =]

I do think I'll stick to Perl or bash for small console scripts though. But then again, I never believed I would switch ove to PHP for the web scripts either but now I do almost all web scriptíng with it.

I've had trouble upgrading PHP before, but now it is a breeze. After I installed the 4.2.2 security release I did a lot of tweaking to get everything right but now I have it just the way I want it. Still have to recompile Apache though, which wasn't fun at all last time, but I think I have come to terms with it now. =] Just have to change some of the settings and compile all modules as dynamic instead of static (which was a mistake anyway).



Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
• Shock Hosting
• Stable Host
• bluehost
• InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,153
Joined: July 2001
Forum Statistics
Forums63
Topics37,583
Posts293,955
Members13,825
Most Online151,614
Nov 14th, 2025
Today's Statistics
Currently Online 4540
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 25
Top Posters
AllenAyres 21,080
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,834
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2026 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.1.0
(Snapshot build 20260108)